package com.pshcp.doctor.tool;

import cn.hutool.core.date.DateUtil;
import com.external.ws.soap.entity.dto.OrderRegisterDTO;
import com.external.ws.soap.entity.dto.PatInfoQueryDTO;
import com.external.ws.soap.entity.res.OrderRegisterRes;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.common.enums.RegStatusEnum;
import com.pshcp.common.enums.RegTypeEnum;
import com.pshcp.common.enums.SchModeEnum;
import com.pshcp.doctor.ao.schedule.DoctorScheduleAO;
import com.pshcp.doctor.ao.schedule.DoctorScheduleConfirmAO;
import com.pshcp.doctor.vo.schedule.DoctorScheduleVO;
import com.pshcp.domain.dto.DoctorScheduleDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.domain.model.Patient;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/6 16:26
 */
public class DoctorScheduleTool {

    public static DoctorScheduleDTO buildGetDoctorScheduleList(Doctor doctor, DoctorScheduleAO doctorScheduleAO){
        DoctorScheduleDTO doctorScheduleDTO=new DoctorScheduleDTO();
        doctorScheduleDTO.setAccessDoctorId(doctor.getHisDoctorId());
        if (!ObjectUtils.isEmpty(doctorScheduleAO.getSchDate())) {
            doctorScheduleDTO.setSchDate(doctorScheduleAO.getSchDate());
        }
        if (!ObjectUtils.isEmpty(doctorScheduleAO.getSchDay())) {
            doctorScheduleDTO.setStartTime(doctorScheduleAO.getSchDate());
            Date schD = DateUtil.parseDate(doctorScheduleAO.getSchDate());
            doctorScheduleDTO.setEndTime(DateUtil.formatDate(DateUtil.offsetDay(schD, doctorScheduleAO.getSchDay())));
            doctorScheduleDTO.setSchDate(null);
        }
        if (!ObjectUtils.isEmpty(doctorScheduleAO.getAccessDeptId())) {
            doctorScheduleDTO.setAccessDeptId(doctorScheduleAO.getAccessDeptId());
        }
        return doctorScheduleDTO;
    }

    public static PatInfoQueryDTO buildPatInfoQueryDTO(String patientCardNo) {
        PatInfoQueryDTO patInfoQueryDTO = new PatInfoQueryDTO();
        patInfoQueryDTO.setPatCardType(0);
        patInfoQueryDTO.setPatCardNo(patientCardNo);
        return patInfoQueryDTO;
    }

    public static OrderRegisterDTO buildWsDTO(DoctorScheduleConfirmAO doctorScheduleConfirmAO, String hisPatientId) {
        OrderRegisterDTO orderRegisterDTO = new OrderRegisterDTO();
        orderRegisterDTO.setAccessPatId(hisPatientId);
        orderRegisterDTO.setSchId(Long.valueOf(doctorScheduleConfirmAO.getHisSchId()));
        orderRegisterDTO.setRegFee(doctorScheduleConfirmAO.getRegFee());
        orderRegisterDTO.setSchMode(SchModeEnum.DOCTOR.getCode() + "");
        orderRegisterDTO.setRegType(RegTypeEnum.NORMAL.getCode());
        orderRegisterDTO.setAccessSchId(doctorScheduleConfirmAO.getHisSchId());
        return orderRegisterDTO;
    }

    public static OrderReg buildWsDTO2Model(OrderRegisterRes registerRes, Doctor doctor, Patient patient) {
        OrderReg orderReg = CommonTool.copyProperties(registerRes, OrderReg.class);
        orderReg.setHisRegId(registerRes.getAccessRegId());
        orderReg.setRegStatus(RegStatusEnum.REG.getCode());
        orderReg.setPayStatus(RegPayStatusEnum.WAIT_PAY.getCode());

        orderReg.setDoctorId(doctor.getId());
        orderReg.setDoctorName(doctor.getName());
        orderReg.setAddress(doctor.getAddress());

        orderReg.setHisPatientId(patient.getHisPatientId());
        orderReg.setPatientUserId(patient.getPatientUserId());
        orderReg.setPatientName(patient.getName());
        orderReg.setPatientId(patient.getId());
        orderReg.setPatPhone(patient.getPhone());
        return orderReg;
    }

    public static void buildArea2ScheduleVO(List<DoctorScheduleVO> list, List<String> areas, Doctor doctor){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        if(CollectionUtils.isEmpty(areas)){
            return;
        }
        Map<String,String> areaMaps = areas.stream().collect(Collectors.toMap(v->v.split("#")[0], v-> v.split("#")[1]));
        list.forEach(item -> {
            item.setTitleShown(doctor.getTitleShown());
            item.setName(doctor.getName());
            boolean isEastArea = item.getDeptName().contains("东院");
            if(isEastArea){
                item.setArea("东院");
                item.setAreaAddr(areaMaps.get(item.getArea()));
            }else{
                item.setArea("西院");
                item.setAreaAddr(areaMaps.get(item.getArea()));
            }
        });
    }

}
